package com.vil.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.vil.common.Constants;
import com.vil.common.ServerResponse;
import com.vil.entity.Admin;
import com.vil.entity.Function;
import com.vil.entity.Role;
import com.vil.service.AdminService;
import com.vil.service.FunctionService;
import com.vil.service.RoleService;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.text.resources.FormatData;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;

@Controller
@RequestMapping("administrator")
public class AdminController extends BaseController{


    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;


    @Autowired
    private FunctionService functionService;


    //去管理员页面
    @RequestMapping("managers.html")
    public String managersList(Model model, HttpSession session) {
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        model.addAttribute("admin", currentAdmin);
        ServerResponse serverResponse = adminService.getListByManyConditions(1, 5, null);
        model.addAttribute("a", serverResponse.getData());
//        model.addAttribute("url","/administrator/adminlist.html");
        return "admin/adminlist";
    }



    //获取管理员列表数据
    @PostMapping("viewlist")
    @ResponseBody
    public ServerResponse viewList(@RequestParam("condition") String condition, @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                   @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize, Model model, HttpSession session) {
//        Admin a = new Admin();
//        a.setLogin_code(condition);
//        return adminService.getListByManyConditions(pageIndex, pageSize, a);
        return adminService.getListByManyCondition(pageIndex, pageSize, condition);
    }

    @RequestMapping("adminadd.html")
    public String adminadd() {

        return "admin/adminadd";
    }


    //增加
    @PostMapping("/addAdmin")
    @ResponseBody
    public ServerResponse addSelective(Admin admin, HttpSession session) {
        return adminService.insertSelective(admin);
    }

    //去编辑页面
    @GetMapping("edit.html/{id}")
    public String toEdit(@PathVariable String id, Model model){
        ServerResponse byId = adminService.getById(Long.valueOf(Integer.parseInt(id)));
        Admin data =(Admin) byId.getData();
        model.addAttribute("u_admin",data);
        return "admin/adminedit";
    }
    //查询
    @GetMapping("getById/{id}")
    @ResponseBody
    public ServerResponse getById(@PathVariable String id, Model model) {

     return   adminService.getById(Long.valueOf(Integer.parseInt(id)));

    }


    //修改
    @PostMapping("editAdmin")
    @ResponseBody
    public ServerResponse editAdmin(Admin admin) {

        return adminService.updateByPrimaryKeySelective(admin);
    }



    @DeleteMapping("deleteById/{id}")
    @ResponseBody
    public ServerResponse deleteById(@PathVariable Long id){

        return  adminService.deleteByPrimaryKey(id);
    }

    @PostMapping("deleteByIds")
    @ResponseBody
    public ServerResponse deleteByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return adminService.deleteMany(array);
    }


    //判断登录名是否已存在
    @PostMapping("isExist")
    @ResponseBody
    public ServerResponse isExist(@RequestParam(value = "login_code",required = true) String login_code){
//        System.out.println("sassaas");
            return adminService.isExist(login_code);
    }


    //去个人信息页面
    @RequestMapping("one_set.html")
    public String oneSet(HttpSession session,Model model){
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        model.addAttribute("a",currentAdmin);
        return "admin/one_set";
    }

    //验证密码是否正确
    @PostMapping("checkPwd")
    @ResponseBody
    public ServerResponse checkPwd(@RequestParam("password") String password,HttpSession session){
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin.getPassword().equals(password)){
            return ServerResponse.buildSuccessMsg("密码一致");
        }
        return ServerResponse.buildErrorMsg("原密码错误");
    }


    /**
     * 用户组列表
     * @param session
     * @param model
     * @return
     */

    @RequestMapping("userGroupManagement.html")
    public String userGroupManagement(HttpSession session,Model model){
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        ServerResponse serverResponse = roleService.getListByManyConditions(1, 5, null);
        model.addAttribute("ag",serverResponse.getData());
        model.addAttribute("admin",currentAdmin);
        return "admin/admingroup";
    }

    @PostMapping("viewgrouplist")
    @ResponseBody
    public ServerResponse viewgrouplist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                        @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                        @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                        Model model, HttpSession session){
        Role r = new Role();
        r.setRole_name(condition);
        return roleService.getListByManyConditions(pageIndex,pageSize,r);
    }
    //删除用户组信息
    @DeleteMapping("deleteGroupById/{id}")
    @ResponseBody
    public ServerResponse deleteGroupById(@PathVariable Long id){
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("该账号未被激活，不能进行此操作");
        }
        return  roleService.deleteByPrimaryKey(id);
    }

    //增加
    @PostMapping("/addGroup")
    @ResponseBody
    public ServerResponse addGroupSelective(Role role, HttpSession session) {
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("该账号未被激活，不能进行此操作");
        }
        return roleService.insertSelective(role);
    }

    //去编辑页面
    @GetMapping("editGroup.html/{id}")
    public String toGroupEdit(@PathVariable Long id, Model model){
        ServerResponse byId = roleService.getById(id);
        Role data =(Role) byId.getData();
        model.addAttribute("u_role",data);
        return "admin/admingroupedit";
    }

    //修改
    @PostMapping("editGroup")
    @ResponseBody
    public ServerResponse editRole(Role role) {
        if (this.isActive()==false){
            return ServerResponse.buildErrorMsg("该账号未被激活，不能进行此操作");
        }
        return roleService.updateByPrimaryKeySelective(role);
    }

    @RequestMapping("admingroupadd.html")
    public String admingroupadd(Model model,HttpSession session) {
        model.addAttribute("a",session.getAttribute(Constants.USER_SESSION));
        return "admin/admingroupadd";
    }


    /**
     * 权限模块
     * @param model
     * @return
     */
    //去权限页面
    @RequestMapping("authorityManagement.html")
    public String authorityManagement(Model model,HttpSession session){
        Admin currentAdmin = (Admin) session.getAttribute(Constants.USER_SESSION);
        if (currentAdmin==null){
            return "admin/login";
        }
        model.addAttribute("admin", currentAdmin);
        ServerResponse serverResponse = functionService.getListByManyConditions(1, 5, null);
        model.addAttribute("authority",serverResponse.getData());
        return "admin/authrule";
    }

    //获取权限列表数据
    @PostMapping("viewauthrolelist")
    @ResponseBody
    public ServerResponse viewauthrolelist(@RequestParam(value = "condition",defaultValue = "",required = false) String condition,
                                           @RequestParam(value = "pageIndex", defaultValue = "1", required = false) int pageIndex,
                                           @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize,
                                           Model model){
        return functionService.getListByManyConditions(pageIndex,pageSize,null);

    }

    //去新增权限页面
    @RequestMapping("auth_ruleadd.html")
    public String auth_ruleadd(){
        return "admin/auth_ruleadd";
    }
    //新增权限
    @PostMapping("auth_ruleadd")
    public ServerResponse addAuthSelective(Function function){

        return functionService.insertSelective(function);
    }
    //去修改权限页面
    @GetMapping("auth_edit.html/{id}")
    public String auth_edit(@PathVariable Long id,Model model){
        ServerResponse byId = functionService.getById(id);
        model.addAttribute("auth",byId.getData());
        return "admin/auth_edit";
    }

    //修改权限
    @PostMapping("editAuth")
    @ResponseBody
    public ServerResponse editAuthSelectvie(Function function){

       return functionService.updateByPrimaryKeySelective(function);
    }

    @PostMapping("deleteAuthByIds")
    @ResponseBody
    public ServerResponse deleteAuthByIds(String ids){

        String str[] = ids.split(",");
        Long array[] = (Long[]) ConvertUtils.convert(str,Long.class);
        return functionService.deleteMany(array);
    }

    //删除权限信息
    @DeleteMapping("deleteAuthById/{id}")
    @ResponseBody
    public ServerResponse deleteAuthById(@PathVariable Long id){

        return  functionService.deleteByPrimaryKey(id);
    }
}
